import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;

/**
 * @Author： yu.Tan
 * @Date: 2020/8/20 9:11
 * @Description: 生产者发送消息的时候指定routing key,然后消费者绑定队列的时候也指定一些binding key，
 * 只有binding key与routing key一致的消费者才能接收到此消息
 */
public class ProducerRouting {

    private static final String EXCHANGE_NAME = "routing_exchange_direct";

    private static final String EXCHANGE_TYPE = "direct";

    private static final String EXCHANGE_ROUTING_KEY = "info";

    public static void main(String[] args) {

        Connection connection = MQConnecitonUtils.getConnection();

        Channel channel = null;

        try {
            channel = connection.createChannel();

            /**
             * @param exchange the name of the exchange
             * @param type the exchange type
             */
            channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);

            String msg = "hello word!";

            channel.basicPublish(EXCHANGE_NAME, EXCHANGE_ROUTING_KEY, null, msg.getBytes("UTF-8"));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != channel) {
                try {
                    channel.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (null != connection) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
